package picobot.simulation;

import java.io.File;

import picobot.bot.Picobot;
import picobot.interfaces.core.IMapBuilder;
import picobot.interfaces.core.IPicobot;
import picobot.map.MapBuilder;
import picobot.rule.RuleBuilder;

/**
 * a class to simulate a Simulator via the console one has to enter 4 arguments:
 * the map file, the rule file, the initial position and the number of steps
 */
public class CommandLineSimulator extends Simulator {

	public static void main(String[] args) {
		int[] init = new int[2];
		init[0] = args[2].charAt(0) - 48;
		init[1] = args[2].charAt(2) - 48;

		Integer steps = new Integer(args[3]);

		CommandLineSimulator com = new CommandLineSimulator(args[0], args[1],
				init, steps);

		for (int i = 0; i < steps; i++) {
			System.out.println(com.getPicobot().getXCoordinate() + ","
					+ com.getPicobot().getYCoordinate());
			com.step();
		}

	}

	/**
	 * the constructor of this class that takes the path of a map and rule file
	 * an integer array and an integer representing the number of steps
	 * 
	 * @param mapFile
	 *            the map file path
	 * @param ruleFile
	 *            the rule file path
	 * @param initialPos
	 *            the initial position as array
	 * @param steps
	 *            the number of steps as integer
	 */
	public CommandLineSimulator(String mapFile, String ruleFile,
			int[] initialPos, int steps) {
		File mFile = new File(mapFile);
		File rFile = new File(ruleFile);

		IMapBuilder mb = new MapBuilder();
		mb.parseMap(mFile);
		loadMap(mb.getCurrentMap());

		IPicobot pico = new Picobot();
		pico.loadRules(new RuleBuilder().parseRules(rFile));
		loadPicobot(pico);

		pico.setInitialPosition(initialPos[0], initialPos[1]);

	}

}
